<template>
  <div class="dialog">
    <el-dialog :title="titleText + '鱼塘'" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="鱼塘名称："  prop="pondName">
          <el-input v-model="formData.pondName" clearable placeholder="请输入鱼塘名称" />
        </el-form-item>
        <el-form-item label="鱼塘面积："  prop="pondArea">
          <el-input v-model="formData.pondArea" type="number" clearable placeholder="请输入鱼塘面积"
            @change="formData.pondArea <= 0 ? formData.pondArea = '' : ''">
            <el-select filterable clearable v-model="formData.areaUnit" allow-create slot="append"
              style="width: 90px;height: 90%;">
              <el-option v-for="item of AreaUnitType" :key="item.name + 'AreaUnitType'" :label="item.value"
                :value="item.name" />
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="NC鱼塘名称"  prop="ncWarehouseid">
          <el-select @change="changeStordoc" class="w100p" filterable clearable v-model="formData.ncWarehouseid" placeholder="请选择NC鱼塘">
            <el-option v-for="(item, index) of StordocList" :key="index + 'ncc'" :label="item.name"
              :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="所在区域："  prop="areaId">
          <el-select class="w100p" filterable clearable v-model="formData.areaId" placeholder="请选择区域">
            <el-option v-for="(item, index) of AreaList" :key="index + 'department'" :label="item.areaName"
              :value="item.areaId" />
          </el-select>
        </el-form-item>
        <el-form-item label="生产能力(斤)："  prop="productivePower">
          <el-input v-model="formData.productivePower" clearable placeholder="请输入生产能力" />
        </el-form-item>
        <el-form-item label="年产值(元)："  prop="annualOutputValue">
          <el-input v-model="formData.annualOutputValue" clearable placeholder="请输入年产值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FishPondApi from "@/api/basic-info-manage/fishPond"
import EnumApi from "@/api/enum"
import AreaApi from "@/api/basic-info-manage/area"
//NC批次
import ncBasicinfoApi from "@/api/nc-basic-info/index";

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value > 0) {
          callback();
        } else {
          return callback(new Error("请输入大于0的数！"));
        }
      }
    };
    return {
      formData: {},
      AreaList: [],
      StordocList:[],//仓库鱼塘
      AreaUnitType: [],
      rules: {
        pondName: [
          { required: true, message: "请输入鱼塘名称", trigger: ["change", "blur"] }
        ],
        pondArea: [
          { required: true, message: "请输入鱼塘面积", trigger: ["change", "blur"] }, {
            validator: checkInt, trigger: ["change", "blur"]
          }
        ],
        areaId: [
          { required: true, message: "请选择", trigger: ["change", "blur"] }
        ],
        ncWarehouseid: [
          { required: true, message: "请选择", trigger: ["change", "blur"] }
        ],
        // productivePower: [
        //   { required: true, message: "请输入 生产能力", trigger: ["change", "blur"] }
        // ],
        // annualOutputValue: [
        //   { required: true, message: "请输入年产值", trigger: ["change", "blur"] }
        // ],
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
          areaUnit: 'Mu',
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryArea()
    this.queryAreaUnit()
    this.queryStordoc()
  },
  methods: {
    // 查询区域
    queryArea() {
      AreaApi.queryByCondition().then(res => {
        this.AreaList = res.data;
      })
    },
    // 查询面积单位
    queryAreaUnit() {
      EnumApi.list({ enumType: 'AreaUnitType' }).then(res => {
        this.AreaUnitType = res.data
      })
    },
        // 查询仓库鱼塘
    queryStordoc() {
      ncBasicinfoApi.queryStordoc({ pk_org: JSON.parse(localStorage.ncOrg) }).then(res => {
        this.StordocList = res.data||[]
      })
    },
    changeStordoc(e){
      let obj = this.StordocList.find(item=>{
        return item.code===e
      })
      this.$set(this.formData,'ncWarehousename',obj.name)

    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            FishPondApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            FishPondApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
